home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Text / Extension Dev. Kit / Extension Sources / Prefix.p < prev    next >
Encoding:
Text File  |  1993-07-24  |  4.3 KB  |  189 lines  |  [TEXT/PJMM]

  1. unit Prefix;
  2. interface
  3.     uses
  4.         DialogUtilities, ExternalInterface;
  5.  
  6.     procedure Main (callbacks: ExternalCallbackPtr; w: WindowPtr);
  7.  
  8. implementation
  9.  
  10.     const
  11.         selOnly = 3;
  12.         insertButton = 4;
  13.         deleteButton = 5;
  14.         beginningOfLine = 6;
  15.         endOfLine = 7;
  16.         prefixString = 8;
  17.  
  18.     var
  19.         prefixInfo: record
  20.                 insert: Boolean;
  21.                 lineStart: Boolean;
  22.                 selOnly: Boolean;
  23.                 pad: Boolean;
  24.  
  25.                 prefixStr: Str255;
  26.             end;
  27.  
  28.     procedure MaintainButtons (d: DialogPtr);
  29.     begin
  30.         SetDlgCtl(d, insertButton, prefixInfo.insert);
  31.         SetDlgCtl(d, deleteButton, not prefixInfo.insert);
  32.  
  33.         SetDlgCtl(d, beginningOfLine, prefixInfo.lineStart);
  34.         SetDlgCtl(d, endOfLine, not prefixInfo.lineStart);
  35.  
  36.         SetDlgCtl(d, selOnly, prefixInfo.selOnly);
  37.     end;
  38.  
  39.     procedure Main (callbacks: ExternalCallbackPtr; w: WindowPtr);
  40.         var
  41.             d: DialogPtr;
  42.             savePort: GrafPtr;
  43.  
  44.             actLen: Integer;
  45.  
  46.             selEnd, selStart, firstChar: LongInt;
  47.             startLine, endLine: LongInt;
  48.             oldStart: LongInt;
  49.  
  50.             item: Integer;
  51.             text: Handle;
  52.  
  53.             s: Str255;
  54.             scratch: Boolean;
  55.  
  56.     begin
  57.         RememberA4;
  58.         SetUpA4;
  59.  
  60.         PrepareCallbacks(callbacks);
  61.  
  62.         GetPort(savePort);
  63.  
  64.         d := CenterDialog(128);
  65.         SetPort(d);
  66.  
  67.         GetSelection(selStart, selEnd, firstChar);
  68.  
  69.         GetPreference('Prfx', SizeOf(prefixInfo), @prefixInfo, actLen);
  70.  
  71.         if (actLen <= 0) then
  72.             begin
  73.                 prefixInfo.insert := TRUE;
  74.                 prefixInfo.lineStart := TRUE;
  75.                 prefixInfo.prefixStr := '> ';
  76.                 prefixInfo.selOnly := (selEnd <> selStart);
  77.             end;
  78.  
  79.         XAbleDlgCtl(d, selOnly, prefixInfo.selOnly);
  80.         SetStrItem(d, prefixString, prefixInfo.prefixStr);
  81.         SelIText(d, prefixString, 0, 255);
  82.  
  83.         repeat
  84.             MaintainButtons(d);
  85.             ModalDialog(@StandardFilter, item);
  86.  
  87.             case item of
  88.                 insertButton, deleteButton: 
  89.                     prefixInfo.insert := (item = insertButton);
  90.  
  91.                 beginningOfLine, endOfLine: 
  92.                     prefixInfo.lineStart := (item = beginningOfLine);
  93.  
  94.                 selOnly: 
  95.                     prefixInfo.selOnly := not prefixInfo.selOnly;
  96.             end;
  97.         until (item = ok) or (item = cancel);
  98.  
  99.         ReadStrItem(d, prefixString, prefixInfo.prefixStr);
  100.         DisposDialog(d);
  101.  
  102.         SetPort(savePort);
  103.  
  104.         if (item = ok) then
  105.             begin
  106.                 SetPreference('Prfx', SizeOf(prefixInfo), @prefixInfo, actLen);
  107.  
  108.                 startLine := 0;
  109.                 endLine := GetLastLine;
  110.  
  111.                 if (prefixInfo.selOnly) then
  112.                     begin
  113.                         startLine := GetLineNumber(selStart);
  114.                         endLine := GetLineNumber(selEnd);
  115.                     end;
  116.  
  117.                 if (Length(prefixInfo.prefixStr) <> 0) then
  118.                     begin
  119.                         s := prefixInfo.prefixStr;
  120.  
  121.                         text := GetWindowContents(w);
  122.                         StartProgress('Changing Lines…', endLine - startLine, FALSE);
  123.                         oldStart := startLine;
  124.  
  125.                         if (prefixInfo.lineStart) then
  126.                             begin
  127.                                 if (prefixInfo.insert) then
  128.                                     while (startLine < endLine) do
  129.                                         begin
  130.                                             scratch := DoProgress(startLine - oldStart);
  131.                                             selStart := GetLinePos(startLine);
  132.                                             SetSelection(selStart, selStart, firstChar);
  133.                                             InsertText(@s[1], Length(s));
  134.  
  135.                                             startLine := startLine + 1;
  136.                                         end
  137.                                 else
  138.                                     while (startLine < endLine) do
  139.                                         begin
  140.                                             scratch := DoProgress(startLine - oldStart);
  141.                                             selStart := GetLinePos(startLine);
  142.                                             if (FindPattern(text^, selStart + Length(s) + 1, selStart, @s[1], Length(s), FALSE) >= 0) then
  143.                                                 begin
  144.                                                     SetSelection(selStart, selStart + Length(s), firstChar);
  145.                                                     DeleteText;
  146.                                                 end;
  147.  
  148.                                             startLine := startLine + 1;
  149.                                         end
  150.                             end
  151.                         else
  152.                             begin
  153.                                 if (prefixInfo.insert) then
  154.                                     while (startLine < endLine) do
  155.                                         begin
  156.                                             scratch := DoProgress(startLine - oldStart);
  157.  
  158.                                             selStart := GetLinePos(startLine);
  159.                                             selStart := GetLineEnd(selStart);
  160.                                             SetSelection(selStart, selStart, firstChar);
  161.                                             InsertText(@s[1], Length(s));
  162.  
  163.                                             startLine := startLine + 1;
  164.                                         end
  165.                                 else
  166.                                     while (startLine < endLine) do
  167.                                         begin
  168.                                             scratch := DoProgress(startLine - oldStart);
  169.  
  170.                                             selStart := GetLinePos(startLine);
  171.                                             selEnd := GetLineEnd(selStart);
  172.  
  173.                                             if (FindPattern(text^, selEnd, selEnd - Length(s), @s[1], Length(s), FALSE) >= 0) then
  174.                                                 begin
  175.                                                     SetSelection(selEnd - Length(s), selEnd, firstChar);
  176.                                                     DeleteText;
  177.                                                 end;
  178.  
  179.                                             startLine := startLine + 1;
  180.                                         end;
  181.                             end;
  182.  
  183.                         DoneProgress;
  184.                     end;
  185.             end;
  186.  
  187.         RestoreA4;
  188.     end;
  189. end.